<?php

global $_GPC;
global $_W;
$shopid = check_right(2);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
if ($operation == 'display') {
    $goodpcate = pdo_fetchall('SELECT id,title FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by id desc');
    $goodary = array();
    foreach ($goodpcate as $row) {
        $goodary[$row['id']] = $row['title'];
    }
} else {
    if ($operation == 'getinfo') {
        $condition = ' and groupid =\'' . $shopid . '\' ';
        intval($_GPC['datetype']);
        switch (intval($_GPC['datetype'])) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
        }
        $list_id = pdo_fetchall('SELECT id FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and ispay=1');
        $idary = array();
        foreach ($list_id as $row) {
            $idary[] = $row['id'];
        }
        if (!$list_id) {
            exit(json_encode(array('success' => false)));
        }
        $goodinfo = pdo_fetch('SELECT sum(total_fee) as num1,sum(num) as num2 FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid in (' . implode(',', $idary) . ') and ispay=1');
        $goodinfo2 = pdo_fetchall('SELECT sum(total_fee) as num1,sum(num) as num2,title,goodid FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid in (' . implode(',', $idary) . ') and ispay=0 group by goodid order by num1 desc ,num2 desc,id desc limit 10');
        $sql = 'SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.pcate FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE a.orderid in (' . implode(',', $idary) . ') and a.ispay=1 group by b.pcate';
        $goodinfo3 = pdo_fetchall($sql);
        exit(json_encode(array('success' => true, 'num' => count($idary), 'goodinfo' => $goodinfo, 'goodinfo2' => $goodinfo2, 'goodinfo3' => $goodinfo3)));
    } else {
        if ($operation == 'outputs') {
            $condition = ' and groupid =\'' . $shopid . '\' ';
            intval($_GPC['datetype']);
            switch (intval($_GPC['datetype'])) {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
            $list_id = pdo_fetchall('SELECT id FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and ispay=1');
            $idary = array();
            foreach ($list_id as $row) {
                $idary[] = $row['id'];
            }
            if (!$list_id) {
                exit(json_encode(array('success' => false)));
            }
            $goodlist = pdo_fetchall('SELECT sum(total_fee) as fee1,sum(num) as num1,max(title) as til,max(title) as til,max(goodsn) as sn,max(oldprice) as price1 FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid in (' . implode(',', $idary) . ') and ispay=1 group by goodid order by sum(total_fee) desc');
            require_once '../framework/library/phpexcel/PHPExcel.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getProperties()->setCreator('智慧收银台')->setLastModifiedBy('智慧收银台')->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('备份数据')->setKeywords('excel')->setCategory('result file');
            $excelAry = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'X', 19 => 'T', 20 => 'U', 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z');
            $headerAry = array(0 => '条码', 1 => '名称', 2 => '原单价', 3 => '销售金额', 4 => '数量', 5 => '实销单价', 6 => '折扣率');
            $i = 0;
            while ($i < count($headerAry)) {
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($excelAry[$i] . '1', $headerAry[$i]);
                $i = $i + 1;
            }
            $num = 2;
            foreach ($goodlist as $row) {
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $num, '\'' . $row['sn'])->setCellValue('B' . $num, $row['til'])->setCellValue('C' . $num, $row['price1'])->setCellValue('D' . $num, $this->_2money($row['fee1']))->setCellValue('E' . $num, $row['num1'])->setCellValue('F' . $num, $this->_2money($row['fee1'] / $row['num1']))->setCellValue('G' . $num, $this->_2money($row['fee1'] / $row['num1'] / ($row['price1'] * 100) * 100 * 100) . '%');
                $num = $num + 1;
            }
            $objPHPExcel->getActiveSheet()->setTitle('商品销售记录');
            $objPHPExcel->setActiveSheetIndex(0);
            ob_end_clean();
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition:attachment;filename="salegoods_temp.xls"');
            header('Cache-Control:max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit(0);
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/salegoods');